$(function () {

    var permissions = $("#permissions"),
        roleSpan = $("#roleSpan"),
        permissionPanel = $("#permissionPanel"),
        // 
        employeeId = $("#content").data('id'),
        employee_admin = $("#content").data('ea'),
        admin = $("#content").data('admin'),
        loginName = $("#content").data('loginname'),
        group = {},
        pColor = {
            '员工相关权限': 'bg-info',
            '用户相关权限': 'bg-system'
        };

    /**
     * 加载当前登录员工权限列表
     */
    var loanEmployeePermissions = function () {
        $.get("employee/permissions/" + employeeId, function (permissions) {
            if (permissions && permissions.length > 0) {
                permissionPanel.html("");
                for (var i = 0; i < permissions.length; i++) {
                    var permission = permissions[i], span = $('<span class="label label-info label-xs light mr10 mb10 pull-left"></span>').addClass(pColor[permission.group]).text(permission.key);
                    permissionPanel.append(span);
                    //$(".permission_checkbox[value=" + permission.name + "]").prop("checked", true);
                }
            } else if (loginName === 'admin') {
                $("#permissionPanel .load-info").html("超级管理员");
            } else {
                $("#permissionPanel .load-info").html("尚未分配权限");
            }
        });
    };
    /**
     * 加载当前登录员工角色
     */
    var loanEmployeeRoles = function () {
        $.get("employee/roles/" + employeeId, function (roles) {
            if (roles && roles.length > 0) {
                for (var i = 0; i < roles.length; i++) {
                    var role = roles[i], span = $('<span class="label bg-alert lighter mr5 mb10 ib lh15"></span>').text(role.name);
                    roleSpan.append(span);
                }
            } else if (loginName === 'admin') {
                var span = $('<span class="label bg-warning mr5 mb10 ib lh15"></span>').text("超级管理员");
                roleSpan.append(span);
            } else {
                var span = $('<span class="label bg-light darker mr5 mb10 ib lh15"></span>').text("尚未分配角色");
                roleSpan.append(span);
            }
        });
    };
    
    var loanEmployeeOperations = function() {
        
        // 初始化列表
        var table = $('#operationList').DataTable({
            "processing": true,
            "serverSide": true,
            "ajax": {
                "url": "operation/listByOperator",
                "data": function (d) {
                    d.realm = "EMPLOYEE";
                    d.id = employeeId;
                },
                "type": "POST"
            },
            "columns": [
                {"data": "operation"},
                {"data": "operation"},
                {"data": "operation"}
            ],
            "aoColumnDefs": [{
                    "targets": 0, //操作类型
                    "mRender": function (data, type, row) {
                        if (data.type in opTypeMap) {
                            return opTypeMap[data.type];
                        } else {
                            return "";
                        }
                    }
                }, {
                    "targets": 1, //操作时间
                    "mRender": function (data, type, row) {
                        return $.format.date(new Date(data.recordTime), "yyyy-MM-dd HH:mm:ss");
                    }
                }, {
                    "targets": 2, //备注
                    "mRender": function (data, type, row) {
                        return data.description;
                    }
                }],
            "order": [1, 'desc'],
            "oLanguage": chinese,
            "language": {
                "decimal": ",",
                "thousands": ",",
            },
            "iDisplayLength": 20,
            "aLengthMenu": [
                [5, 10, 20, 50, 100, -1],
                [5, 10, 20, 50, 100, "全部"]
            ],
            "sDom": 't<"dt-panelfooter clearfix"lp>',
            "initComplete": function (settings, json) {
                $("#operationCount").text(json.recordsTotal);
            }
        });
    }

    var initPage = function () {
        console.log(loginName);
        if (employee_admin === true || admin === true) {
            $(".shide").show();
        }
        loanEmployeeRoles();
        loanEmployeePermissions();
        loanEmployeeOperations();
    };

    // 初始化页面
    initPage();
});